<?php
// +----------------------------------------------------------------------
// | 小牛Admin
// +----------------------------------------------------------------------
// | Website: www.xnadmin.cn
// +----------------------------------------------------------------------
// | Author: dav <85168163@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\UserModel;
use think\exception\ValidateException;

class User extends AdminBase
{
    protected UserModel $model;

    /**
     * 初始化
     * @return void
     */
    public function initialize()
    {
        parent::initialize();
        $this->model = new UserModel();
    }

    /**
     * 会员列表
     */
    public function index()
    {
        if ( $this->request->isAjax() ) {
            $param = $this->request->param();
            $model = new UserModel();
            //构造搜索条件
            if( trim($param['keyword'])!='' ) {
                $model = $model->where('username|nickname|mobile|email|intro','like',"%".trim($param['keyword'])."%");
            }
            if( $param['sex']!='' ) {
                $model = $model->where('sex',$param['sex']);
            }
            if( $param['start_date']!=''&&$param['end_date']!='' ) {
                $start_date = $param['start_date'].' 00:00:00';
                $end_date = $param['end_date'].' 23:59:59';
                $model = $model->whereBetweenTime('create_time',$start_date,$end_date);
            }
            $list = $model->order('id desc')
                ->paginate([
                    'query' => $param,
                    'list_rows'=>$this->request->param('limit',20)
                ]);
            $this->success('获取成功', '', $list);
        }
        return view();
    }

    /**
     * 添加会员
     */
    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            try {
                validate(\app\common\validate\Login::class)
                    ->scene('register')
                    ->check($param);
            } catch (ValidateException $e) {
                $this->error($e->getMessage());
            }
            if ( isset($param['password']) && $param['password']!='') {
                $salt = substr(md5(uniqid(true)), 0, 6);
                $param['salt'] = $salt;
                $param['password'] = xn_encrypt($param['password'],$salt);
            }
            $result = $this->model->save($param);
            if( $result ) {
                xn_add_admin_log('添加会员');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        return view('form');
    }

    /**
     * 编辑会员
     */
    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            if ( isset($param['password']) && $param['password']!='') {
                $salt = $this->model->where('id',$param['id'])->value('salt');
                $param['password'] = xn_encrypt($param['password'],$salt);
            }
            $result = $this->model->update($param);
            if( $result ) {
                xn_add_admin_log('编辑会员');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->get('id');
        $_data = $this->model->find($id);
        $data = $_data->toArray();
        
        return view('form',compact('data'));
    }

    /**
     * 删除会员
     */
    public function delete()
    {
        $id = $this->request->param('id');
        $this->model->destroy($id);
        xn_add_admin_log('删除会员');
        $this->success('删除成功');
    }
}